package com.example.listossimage.死锁案例;

import org.junit.Test;

public class Lock {

    @Test
    public void testLock() {

        Object object = new Object();
        Object object1 = new Object();

        while (true) {
            new Thread() {
                @Override
                public void run() {
                    synchronized (object) {
                        try {
                            System.out.println("线程1");
                            Thread.sleep(1000);
                        }catch (Exception e) {
                            e.printStackTrace();
                        }
                        synchronized (object1) {
                            System.out.println("占用了2的锁");
                        }
                    }
                }
            }.start();

            new Thread() {
                @Override
                public void run() {
                    synchronized (object1) {
                        try {
                            System.out.println("线程2");
                            Thread.sleep(1000);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        synchronized (object) {
                            System.out.println("占用了1的锁");
                        }
                    }
                }
            }.start();
        }
    }

    @Test
    public void test1() {

    }
}
